﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneralDFS.DataEntity.IO;
using GeneralDFS.DataEntity.Nodes;

namespace GeneralDFS.BusinessLogical.IO
{
    public class BlockIterator : IEnumerable, IEnumerator<BlockIterator>
    {
        public BlockIterator(FileBlockInfo block, DataNodeInfoWithBlocks dataNodeInfo)
        {
            currentBlockInfo = block;
            firstBlockInfo = block;
            DataNodeInfoWithBlocks = dataNodeInfo;
        }
        public void Dispose()
        {

        }

        public bool MoveNext()
        {
            lock (currentBlockInfo)
            {
                try
                {
                    currentBlockInfo = Current.currentBlockInfo.GetNext(Current.currentBlockInfo.FindDataNodeInfo(DataNodeInfoWithBlocks));
                    Current = this;
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public void Reset()
        {
            lock (currentBlockInfo)
            {
                currentBlockInfo = firstBlockInfo;
            }
        }

        public BlockIterator Current { get; private set; }

        private FileBlockInfo firstBlockInfo;
        public FileBlockInfo currentBlockInfo;
        private DataNodeInfoWithBlocks DataNodeInfoWithBlocks;

        object IEnumerator.Current
        {
            get { return currentBlockInfo; }
        }

        public IEnumerator GetEnumerator()
        {
            return this;
        }
    }
}
